let app = getApp();
Page({
    data: {
       header:{},
       userInfo:{}, 
       title:'',
       content:'',
       changeId:null,
       linshiArr:[],
       "province": "湖北省",
       "city": "请选择",
       item:'武汉',
       choosetype:'一般',
       choosetype2:'一般',
       choosetype3:'有',
       choosetype4:'有',


   },
    turnCity:function(){
      wx.navigateTo({
        url: '/pages/cityChoose/webView?type=4'
      })
    },
   onLoad: function(options) {
     var address= wx.getStorageSync('address');
        this.setData({
            address:address,
            city:address.city,
            item:address.city,
            province:address.province
      })
     this.setData({
          type:options.type
     })   
     getApp().getUserInfo().then(res=>{
      var header={
             'openid':res.openid, //公用请求头
             'content-type': 'application/json'
         }
       this.setData({
        userInfo:res,
        header:header
       })  
       this.getNoReady();
     })
     
    },
    getNoReady:function(){
     //     var _self=this;
     //     wx.request({
     //        url:getApp().globalData.baseUrl+'/wx/yxj/equipment/getList?status='+this.data.select+'&openid='+this.data.userInfo.openid+'&current='+this.data.current+'&pagesize='+this.data.pagesize,
     //        method:"GET",
     //        header: {
     //            'openid':this.data.userInfo.openid, //公用请求头
     //            'content-type': 'application/json'
     //        },
     //        success: function(res) {
     //         _self.setData({
     //            showList:_self.data.showList.concat(res.data.obj.list),
     //            pages:res.data.obj.pages
     //         })
             
     //     }
     // })
    },
    onReady: function() {
        // 生命周期函数--监听页面初次渲染完成

    },
     onMyEvent:function(province){
      console.log(province.detail)
      this.setData({
        province:province.detail.split(',')[0],
        city:province.detail.split(',')[1]
      })
      
     
    },
    onShow: function() {
     
       var chooseProvince= wx.getStorageSync('chooseProvince4');
       var chooseCity2=wx.getStorageSync('chooseCity4');
     
      
        if (chooseProvince) {
          this.setData({
            city:wx.getStorageSync('chooseCity4')||'全国',
            item:wx.getStorageSync('chooseCity4')||'全国',
            province:wx.getStorageSync('chooseProvince4')||'全国'
          })
        }

       var type1= wx.getStorageSync('typel3');
     
       if (type1) {
          this.setData({
            linshiArr:type1.itemUrlList,
            sellerMsg:type1.content,
            choosetype:type1.itemChildListList[0].content,
            choosetype2:type1.itemChildListList[1].content,
            choosetype3:type1.itemChildListList[2].content
           

          })
       } 
    

    },
    
      
   
    onHide: function() {
        // 生命周期函数--监听页面隐藏

    },
    textTitle:function (e) {
      console.log(e.detail.value)
       this.setData({
         title:e.detail.value
       })
    },
    //输入价格
    textPrice:function (e) {
       this.setData({
        price:e.detail.value
      })
    },
    textContentSeller:function (e) {
       console.log(e.detail.value)
       this.setData({
         sellerMsg:e.detail.value
       })
    },
    textContentButler:function (e) {
       console.log(e.detail.value)
       this.setData({
         butlerMsg:e.detail.value
       })
    },
    sendSheQu: function(){
      if (!this.data.sellerMsg) {
          wx.showToast({
              title: '请输入描述',
              icon:'none',
              duration: 2000
            })
            return
      }

       var json= {
        "type":3,
        "content":this.data.sellerMsg,
         "itemChildListList":[
          {"type":10,"status":this.data.choosetype=='一般'?0:1,"content":this.data.choosetype},
          {"type":20,"status":this.data.choosetype2=='一般'?0:1,"content":this.data.choosetype2},
          {"type":30,"status":this.data.choosetype3=='无'?0:1,"content":this.data.choosetype3}],
          "itemUrlList":this.data.linshiArr
        }
        console.log('******')
        console.log(json)
         wx.setStorageSync('typel3',json); 
         wx.navigateBack({
            delta: 1
          })
        return
   


       if (this.data.linshiArr.length>6) {
             wx.showToast({
              title: '最多上传3张图片',
              icon:'none',
              duration: 2000
            })
             var  yarr=this.data.linshiArr.slice(0,6);

             this.setData({
               linshiArr:yarr
             })
      
       }
       console.log(this.data.linshiArr)
       wx.request({
            url: getApp().globalData.baseUrl + '/wx/yxj/equipment/add',
            method:"POST",
            data:{
                openid:this.data.userInfo.openid,
                type:this.data.type,
                price:this.data.price,
                butlerMsg:this.data.butlerMsg,
                sellerMsg:this.data.sellerMsg,
                url:this.data.linshiArr[0].url,
                isTestMachine:0,
                province:"湖北省",
                city:'武汉',
                "status":50,
                itemList:[]
                // // "hotTopicType": this.data.hotTopicType,//主题
                // "title": this.data.title,//标题
                // "content": this.data.content,//内容
                // "province": this.data.province,
                // "city":this.data.city,
                // "area": "",
                // "latitude": "",
                // "longitude": "",
                // "wxArticlesPic":this.data.linshiArr,
                
            },
            header: this.data.header,
            success: function(res) {
               console.log(res)
                wx.setStorageSync('chooseProvince4',null);
                wx.setStorageSync('chooseCity4',null);
              wx.switchTab({
                  url: '../../pages/mine/mine'
                })
                                    
            }
            })

    },chooseImage:function () {
      var _self=this;
      wx.chooseImage({
        success (res) { 
          const tempFilePaths = res.tempFilePaths
          console.log(tempFilePaths[0])
          var imgUrl=tempFilePaths[0];
          console.log('##################')
          for (var i = tempFilePaths.length - 1; i >= 0; i--) {
              _self.uploadimg(tempFilePaths[i]);
              console.log(i)
          }
         
        }
      })
    },
    uploadimg:function (path) {
      var _self=this;
       wx.uploadFile({
             url:getApp().globalData.baseUrl+'/wx/img/url', //仅为示例，非真实的接口地址
            //url:getApp().globalData.baseUrl+'/imagesnode/uploadImg',
            filePath: path,
            header: {
              'content-type':'multipart/form-data',
              'openid':_self.data.userInfo.openid 
            },
            name: 'filePath',
            formData: {
              openid:_self.data.userInfo.openid 
            },
            success (res){
             var data = JSON.parse(res.data);
             console.log(data.obj);
              var arr=_self.data.linshiArr;
              var json={url:data.obj}
              arr.push(json)
              
                _self.setData({
                    linshiArr:arr
                })
              //do something
            }
          })
    },
    //选中设备类型
    chooseType:function (e) {
        console.log('%***********')
        console.log(e.target.dataset.code)
        this.setData({
            choosetype:e.target.dataset.code
        })
    },
    //选中设备类型
    chooseType2:function (e) {
        console.log('%***********')
        console.log(e.target.dataset.code)
        this.setData({
            choosetype2:e.target.dataset.code
        })
    },
    //选中设备类型
    chooseType3:function (e) {
        console.log('%***********')
        console.log(e.target.dataset.code)
        this.setData({
            choosetype3:e.target.dataset.code
        })
    },
    //选中设备类型
    chooseType4:function (e) {
        console.log('%***********')
        console.log(e.target.dataset.code)
        this.setData({
            choosetype4:e.target.dataset.code
        })
    },
    deleteImg:function (e) {
       console.log(e.target.dataset.code);
       var index=e.target.dataset.code;
       var arr=this.data.linshiArr;
       arr.splice(index,1);
       this.setData({
        linshiArr:arr
       })
    }

})